package day26;

import day7.Phone;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo5 {
}
//反射实现通用方法的调用
class Test{
    public static Object base(Object o,String name,Object ...  params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Class c=o.getClass();
        Class args[]=new Class[params.length];
        for (int i=0;i< args.length;i++){
            args[i]=params[i].getClass();
        }
        Method m=c.getDeclaredMethod(name,args);
        m.setAccessible(true);
        Object result=m.invoke(o,params);
        return result;
    }

    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {
        Phone p=new Phone();
        Object res= Test.base(p,"setType","鸿蒙");
        System.out.println(res);
        Person p2=new Person();
        Test.base(p2,"study");
        Test.base(p2,"play","手机");
        Test.base(p2,"play","张三","电脑");
    }
}